/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.config.VideoInfoBean;
import com.yc.video.player.SimpleStateListener;
import com.yc.video.player.VideoPlayer;
import com.yc.video.ui.view.BasisVideoController;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.ResourceTable;

import java.util.List;

/**
 * 连续播放列表视频
 * 意思是说播放完了第一个，接着播放第二个，第三个……
 *
 * @since 2021-05-10
 */
public class ContinuousVideoAbility extends BaseAbility implements Component.ClickedListener {
    private VideoPlayer mVideoPlayer;
    private Button mBtnScaleNormal;
    private Button mBtnScale169;
    private Button mBtnScale43;
    private Button mBtnCrop;
    private Button mBtnGif;
    private List<VideoInfoBean> data = ConstantVideo.getVideoList();

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_normal_video;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        mVideoPlayer = (VideoPlayer) findComponentById(ResourceTable.Id_video_player);
        mBtnScaleNormal = (Button) findComponentById(ResourceTable.Id_btn_scale_normal);
        mBtnScale169 = (Button) findComponentById(ResourceTable.Id_btn_scale_169);
        mBtnScale43 = (Button) findComponentById(ResourceTable.Id_btn_scale_43);
        mBtnCrop = (Button) findComponentById(ResourceTable.Id_btn_crop);
        mBtnGif = (Button) findComponentById(ResourceTable.Id_btn_gif);
        initVideoPlayer();
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (mVideoPlayer != null) {
            mVideoPlayer.resume();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mVideoPlayer != null) {
            mVideoPlayer.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoPlayer != null) {
            mVideoPlayer.release();
        }
    }

    @Override
    protected void onBackPressed() {
        ControlWrapper.defaultValue();
        if (mVideoPlayer == null || !mVideoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }

    private void initVideoPlayer() {
        mController = new BasisVideoController(this);
        mController.getThumb().setPixelMap(ResourceTable.Media_image_default);
        // 设置控制器
        mVideoPlayer.setController(mController);
        mVideoPlayer.setUrl(ConstantVideo.VideoPlayerList.get(0));
        mVideoPlayer.start();
        // 监听播放结束
        mVideoPlayer.addOnStateChangeListener(new SimpleStateListener() {
            private int mCurrentVideoPosition;

            @Override
            public void onPlayStateChanged(int playState) {
                if (playState == ConstantKeys.CurrentState.STATE_BUFFERING_PLAYING) {
                    if (data != null) {
                        mCurrentVideoPosition++;
                        if (mCurrentVideoPosition >= data.size()) {
                            return;
                        }
                        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                            @Override
                            public void run() {
                                mVideoPlayer.release();
                                // 重新设置数据
                                VideoInfoBean videoBean = data.get(mCurrentVideoPosition);
                                mVideoPlayer.setUrl(videoBean.getVideoUrl());
                                mVideoPlayer.setController(mController);
                                // 开始播放
                                mVideoPlayer.start();
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBtnScaleNormal.setClickedListener(this);
        mBtnScale169.setClickedListener(this);
        mBtnScale43.setClickedListener(this);
        mBtnCrop.setClickedListener(this);
        mBtnGif.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (component == mBtnScale169) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_16_9);
        } else if (component == mBtnScaleNormal) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_DEFAULT);
        } else if (component == mBtnScale43) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_4_3);
        }
    }
}
